package com.huawei.appmarket.service.externalapi.actions;

import android.os.AsyncTask.Status;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.TextUtils;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.secure.android.common.intent.SafeIntent;
import o.cfw;
import o.cvt;
import o.BaseRequest;
import o.AsyLoader;
import o.Logger;
import o.dok;
import o.dpb;
import o.dyi;
import o.dyr;
import o.dys.a;
import o.epr;

public class ExtPublicAction extends dyr {
    private static final long DELAY_DUR = 30;
    private static final int MAX_DELAY_COUNT = 3;
    private static final String TAG = "ExtPublicAction";
    private static final long TASK_EXCUTE_TIMEOUT_TIME = 8000;
    private int delayCount = 1;
    private String openStr;
    private AsyLoader task;
    private String thirdId;

    public ExtPublicAction(a aVar) {
        super(aVar);
    }

    public boolean isNeedLoading() {
        return true;
    }

    public long getTimeout() {
        return TASK_EXCUTE_TIMEOUT_TIME;
    }

    public void onAction() {
        showMainLayout();
    }

    public void onDestroy() {
        cancelTask();
    }

    private void showMainLayout() {
        SafeIntent safeIntent = new SafeIntent(this.callback.getIntent());
        this.openStr = safeIntent.getStringExtra("openStr");
        if (TextUtils.isEmpty(this.openStr)) {
            Logger.e(TAG, "openStr is null.");
            this.callback.finish();
            return;
        }
        this.thirdId = safeIntent.getStringExtra("thirdId");
        epr.ˎ().ˎ(this.thirdId);
        Logger.ˎ(TAG, "thirdId:" + this.thirdId + ",openStr:" + this.openStr);
        if (dok.ͺ()) {
            openActivity();
        } else if (!dpb.ˊ(this.callback, this.openStr)) {
            this.delayCount = 1;
            postDelay(DELAY_DUR);
        }
        dailyReport(this.thirdId);
    }

    private void postDelay(final long j) {
        Logger.ˎ(TAG, "postDelay, delay:" + j + ", delayCount:" + this.delayCount);
        new Handler(new Callback(this) {
            final /* synthetic */ ExtPublicAction ˊ;

            public boolean handleMessage(Message message) {
                if (message.what == 0) {
                    if (dok.ͺ()) {
                        this.ˊ.openActivity();
                    } else if (this.ˊ.delayCount = this.ˊ.delayCount + 1 <= 3) {
                        this.ˊ.postDelay(2 * j);
                    } else {
                        dpb.ˏ(this.ˊ.callback, this.ˊ.openStr);
                    }
                }
                return true;
            }
        }).sendEmptyMessageDelayed(0, j);
    }

    private void openActivity() {
        cvt o_dyi = new dyi();
        o_dyi.openStr_ = this.openStr;
        this.task = cfw.ˎ(o_dyi, new IServerCallBack(this) {
            final /* synthetic */ ExtPublicAction ˎ;

            {
                this.ˎ = r1;
            }

            public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
            }

            public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
                if (!dpb.ˊ(this.ˎ.callback, responseBean)) {
                    this.ˎ.cancelTask();
                    dpb.ˏ(this.ˎ.callback, this.ˎ.openStr);
                }
            }
        });
    }

    public void cancelTask() {
        if (this.task != null) {
            this.task.ˎ(true);
            this.task = null;
        }
    }

    public boolean onTimeout() {
        if (this.task != null && (this.task.getStatus() == Status.RUNNING || this.task.getStatus() == Status.PENDING)) {
            cancelTask();
            dpb.ˏ(this.callback, this.openStr);
        }
        return true;
    }
}
